package com.shujia;

import java.util.ArrayList;
import java.util.BitSet;

public class Code04BL {
    public static void main(String[] args) {
        // 实现简单的布隆过滤器

        int length =1024;
        // 二进制向量
        int[] ints = new int[length];

        // hash函数

        // 如果有X 去其Key

        String xKey = "X";

        addData(ints,xKey);

        findData(ints,xKey);


//        System.out.println(ints[0]);

    }

    private static void findData(int[] ints, String xKey) {
        int length = ints.length;
        int index1 =  xKey.hashCode() % length;
        //
        int hashRes2 =0;
        int seed = 133;

        char[] chars = xKey.toCharArray();
        for (char aChar : chars) {
            hashRes2 =  (aChar + hashRes2) * seed;
        }
        int index2 =  hashRes2 % length;

        int anInt1 = ints[index1];
        int anInt2 = ints[index2];

        if (anInt1 == 1 && anInt2 == 1){
            System.out.println("数据可能在Redis中..");
        }else{
            System.out.println("数据不在在Redis中..");
        }


    }

    public static void addData(int[] ints, String xKey ){
        int length = ints.length;

        int index1 =  xKey.hashCode() % length;

        //
        int hashRes2 =0;
        int seed = 133;

        char[] chars = xKey.toCharArray();
        for (char aChar : chars) {
            hashRes2 =  (aChar + hashRes2) * seed;
        }

        int index2 =  hashRes2 % length;

        ints[index1] = 1;
        ints[index2] = 1;
    }
}
